package boardgamer.yavalath.client.rule;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.MotionEvent;
import boardgamer.common.client.AbstractView;
import boardgamer.common.client.layout.HasLayout;
import boardgamer.common.client.layout.LayoutGroup;
import boardgamer.common.client.layout.MultilineTextLayout;
import boardgamer.common.client.layout.TextLayout;
import boardgamer.yavalath.client.Header;
import boardgamer.yavalath.client.resources.ImageHandler;
import boardgamer.yavalath.client.resources.StringHandler;

import static android.graphics.Paint.ANTI_ALIAS_FLAG;

/**
 * @author garysoed
 */
public class RuleView extends AbstractView<RuleActivity> {

  private static final float HEADER_HEIGHT_RATIO = 0.06f;
  private static final float TEXT_HEIGHT_RATIO = 0.04f;
  private static final float TEXT_LINE_HEIGHT_RATIO = 0.05f;

  private static final float HEADER_TOP_RATIO = 0.2f;
  private static final float HEADER_BOTTOM_GAP_RATIO = 0.02f;
  private static final float PARAGRAPH_GAP_RATIO = 0.04f;
  private static final float INDENT_RATIO = 0.1f;

  private Header header;

  private TextLayout objectiveHeader;
  private MultilineTextLayout objectiveText;

  private TextLayout ruleHeader;
  private MultilineTextLayout ruleText1;
  private MultilineTextLayout ruleText2;

  private TextLayout threePlayersHeader;
  private MultilineTextLayout threePlayersText;

  protected RuleView(RuleActivity activity) {
    super(activity);
  }

  private void addIndent(Rect canvasRect, HasLayout layoutObject) {
    RectF boundingRect = layoutObject.getBoundingRect();
    boundingRect.offset(canvasRect.width() * INDENT_RATIO, 0);
    layoutObject.setBoundingRect(boundingRect);
  }

  @Override
  protected void handleDraw(Canvas canvas) {
    header.draw(canvas);
    objectiveHeader.draw(canvas);
    objectiveText.draw(canvas);
    ruleHeader.draw(canvas);
    ruleText1.draw(canvas);
    ruleText2.draw(canvas);
    threePlayersHeader.draw(canvas);
    threePlayersText.draw(canvas);
  }

  @Override
  protected void handleUpdate(int elapsedTime) {
    // TODO: Add background
    //To change body of implemented methods use File | Settings | File Templates.
  }

  @Override
  protected void handleLayout(Rect canvasRect) {
    StringHandler stringHandler = StringHandler.instance();
    this.header = new Header(stringHandler.ruleTitle, canvasRect);

    float normalTextBoundHeight
        = ImageHandler.boundSizeRatio.get(ImageHandler.normalFont).height();

    Paint headerPaint = new Paint(ANTI_ALIAS_FLAG);
    headerPaint.setColor(Color.RED);
    headerPaint.setTypeface(ImageHandler.normalFont);
    headerPaint.setTextSize(canvasRect.height() * HEADER_HEIGHT_RATIO / normalTextBoundHeight);

    Paint textPaint = new Paint(ANTI_ALIAS_FLAG);
    textPaint.setColor(Color.rgb(128, 0, 255));
    textPaint.setTypeface(ImageHandler.normalFont);
    textPaint.setTextSize(canvasRect.height() * TEXT_HEIGHT_RATIO / normalTextBoundHeight);

    float textWidth = canvasRect.width() * (1 - INDENT_RATIO);
    float textLineHeight = canvasRect.height() * TEXT_LINE_HEIGHT_RATIO;

    this.objectiveHeader = new TextLayout(stringHandler.ruleObjectiveHeader, headerPaint);
    this.objectiveText = new MultilineTextLayout(
        stringHandler.ruleObjectiveText,
        textPaint,
        textWidth,
        textLineHeight,
        LayoutGroup.HorizontalAlign.LEFT);

    this.ruleHeader = new TextLayout(stringHandler.ruleGeneralHeader, headerPaint);
    this.ruleText1 = new MultilineTextLayout(
        stringHandler.ruleGeneralText1,
        textPaint,
        textWidth,
        textLineHeight,
        LayoutGroup.HorizontalAlign.LEFT);
    this.ruleText2 = new MultilineTextLayout(
        stringHandler.ruleGeneralText2,
        textPaint,
        textWidth,
        textLineHeight,
        LayoutGroup.HorizontalAlign.LEFT);

    this.threePlayersHeader = new TextLayout(stringHandler.rule3PlayersHeader, headerPaint);
    this.threePlayersText = new MultilineTextLayout(
        stringHandler.rule3PlayersText,
        textPaint,
        textWidth,
        textLineHeight,
        LayoutGroup.HorizontalAlign.LEFT);

    float headerBottomGap = canvasRect.height() * HEADER_BOTTOM_GAP_RATIO;
    float paragraphGap = canvasRect.height() * PARAGRAPH_GAP_RATIO;

    new LayoutGroup()
        .add(objectiveHeader).atPosition(0, canvasRect.height() * HEADER_TOP_RATIO)
        .add(objectiveText)
        .relativeToEdge(objectiveHeader, LayoutGroup.Direction.BOTTOM, headerBottomGap)
        .add(ruleHeader).relativeToEdge(objectiveText, LayoutGroup.Direction.BOTTOM, paragraphGap)
        .add(ruleText1).relativeToEdge(ruleHeader, LayoutGroup.Direction.BOTTOM, headerBottomGap)
        .add(ruleText2).relativeToEdge(ruleText1, LayoutGroup.Direction.BOTTOM, headerBottomGap)
        .add(threePlayersHeader)
        .relativeToEdge(ruleText2, LayoutGroup.Direction.BOTTOM, paragraphGap)
        .add(threePlayersText)
        .relativeToEdge(threePlayersHeader, LayoutGroup.Direction.BOTTOM, headerBottomGap);

    addIndent(canvasRect, objectiveText);
    addIndent(canvasRect, ruleText1);
    addIndent(canvasRect, ruleText2);
    addIndent(canvasRect, threePlayersText);
  }

  @Override
  protected void handleTouchEvent(MotionEvent event) { }
}
